package com.theme.passport.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.validator.ValidatorUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.theme.SearchHistoryEntity;
import com.theme.passport.form.SearchForm;
import com.theme.passport.service.theme.SearchHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("app/search")
@Api("用户搜索主题记录")
public class SearchHistoryController {
    @Autowired
    private SearchHistoryService searchHistoryService;

    @Login
    @GetMapping("/list")
    @ApiOperation("查询用户搜索记录接口")
    public R list(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = searchHistoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/hot")
    @ApiOperation("查询热门搜索关键词接口")
    public R hot(@RequestParam Map<String, Object> params){
        List<String> keywordList = new ArrayList<>();
        List<Map<String, Object>> resultListMap = searchHistoryService.querySearchHotKeyword(params);
        if (resultListMap != null && resultListMap.size() > 0){
            resultListMap.forEach(objectMap -> {
                String keyword = objectMap.get("keyword") + "";
                keywordList.add(keyword);
            });
        }
        return R.ok().put("keywordList", keywordList);
    }


    @Login
    @PostMapping("/add")
    @ApiOperation("添加用户搜索记录的接口")
    public R add(@RequestBody SearchForm form){

        //表单校验
        ValidatorUtils.validateEntity(form);

        SearchHistoryEntity searchHistory = new SearchHistoryEntity();
        searchHistory.setKeyword(form.getKeyword());

        UserEntity currentUser = AppContextUtil.getCurrentUser();
        QueryWrapper<SearchHistoryEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper.eq("user_id", currentUser.getId()).eq("keyword", searchHistory.getKeyword()));
        List<SearchHistoryEntity> list = searchHistoryService.list(orderWrapper);
        if (list != null && list.size()>0){
            return R.ok();
        }
        searchHistory.setCreated(new Date());
        searchHistory.setUserId(currentUser.getId());
        searchHistoryService.save(searchHistory);

        return R.ok();
    }


    @Login
    @PostMapping("/delete")
    @ApiOperation("删除用户搜索记录接口 id数组")
    public R delete(@RequestBody Long[] ids){
		searchHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
